package com.guapi.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.guapi.annotation.SelfController;
import com.guapi.dao.Menu;
import com.guapi.dao.ResponseData;
import com.guapi.dao.User;
import com.guapi.exception.SelfException;
import com.guapi.mapper.wrapper.SelfQueryWrapper;
import com.guapi.service.MenuService;
import com.guapi.util.request.SelfRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@SelfController
@RequestMapping("/menu")
@ResponseBody
public class MenuController extends BaseController<Menu> {

    @Autowired
    private MenuService menuService;

    @RequestMapping("/list")
    public ResponseData getMenuList(HttpServletRequest request, @RequestBody Map data) {
        ResponseData responseData = new ResponseData();
        startPage(data);
        List<Menu> menuList;
        String menuName = "";
        String path = "";
        Map searchStr = (Map) data.get("searchStr");
        if (searchStr != null) {
            menuName = (String) searchStr.get("menuName");
            path = (String) searchStr.get("path");
        }
        long total = 0;
        if (StrUtil.isAllEmpty(menuName, path)) {
            menuList = menuService.list();
            total = (long) menuList.size();
        } else {
            Menu menu = new Menu();
            if (StrUtil.isNotEmpty(menuName)) {
                menu.setMenuName(menuName);
            }
            if (StrUtil.isNotEmpty(path)) {
                menu.setPath(path);
            }
            menuList = menuService.getByFuzzy(menu);
        }
        List<Menu> fatherList = menuService.packageMenu(menuList);
        responseData.setRows(fatherList);
        responseData.setTotal(total);
        responseData.setSuccess(true);
        return responseData;
    }

    @RequestMapping("/save")
    public ResponseData addMenu(HttpServletRequest request, @RequestBody Menu menu) {
        SelfRequest selfRequest = createRequest(request);
        if (menu.getParentId().equals("top") && menu.getId() != null && menu.getPath() != null) {
            Menu childMenu = new Menu();
            Menu dbMenu = menuService.getById(menu.getId());
            String dbPath = dbMenu.getPath();

            childMenu.setParentId(String.valueOf(menu.getId()));
            List<Menu> childList = menuService.list(new SelfQueryWrapper<>(childMenu));
            if (!childList.isEmpty()) {
                childList.forEach(menu1 -> {
                    String replace = menu1.getPath().replace(dbPath, "");
                    menu1.setPath(menu.getPath() + replace);
                    menuService.saveOrUpdate(menu1);
                });
            }
        }
        responseData = new ResponseData();
        User loginUser = getLoginUser();
        if (menu.getCreateDate() == null) {
            menu.setCreateBy(String.valueOf(loginUser.getId()));
        } else {
            menu.setUpdateBy(String.valueOf(loginUser.getId()));
        }
        if (menuService.saveOrUpdate(menu)) {
            responseData.setData("menu", menu);
            responseData.setSuccess(true);
        } else {
            throw new SelfException("500", "添加菜单失败，请稍后重试");
        }
        return responseData;

    }

    @RequestMapping("/delete")
    public ResponseData deleteMenu(HttpServletRequest request, @RequestBody Menu menu) {
        SelfRequest selfRequest = createRequest(request);
        responseData = new ResponseData();
        User loginUser = getLoginUser();
        if (CollectionUtil.isNotEmpty(menu.getChildren())) {
            throw new SelfException("当前删除的菜单存在子菜单，请将子菜单删除后重新操作");
        } else {
            menu.setUpdateBy(String.valueOf(loginUser.getId()));
            if (menuService.deleteEntity(menu).equals(menu)) {
                responseData.setSuccess(true);
                responseData.setData("menu", menu);
            }
        }
        return responseData;
    }
}
